<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="OpenID" prop="openid">
        <el-input
          v-model="queryParams.openid"
          placeholder="请输入微信OpenID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="授权类型" prop="authType">
        <el-select v-model="queryParams.authType" placeholder="请选择授权类型" clearable>
          <el-option
            v-for="dict in dict.type.auth_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="授权结果" prop="authResult">
        <el-select v-model="queryParams.authResult" placeholder="请选择授权结果" clearable>
          <el-option
            v-for="dict in dict.type.auth_result"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['wx:authlog:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['wx:authlog:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['wx:authlog:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['wx:authlog:export']"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-delete"
          size="mini"
          @click="handleClean"
          v-hasPermi="['wx:authlog:clean']"
        >清理日志</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="wxAuthLogList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" />
      <el-table-column label="OpenID" align="center" prop="openid" width="280" />
      <el-table-column label="授权类型" align="center" prop="authType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.auth_type" :value="scope.row.authType"/>
        </template>
      </el-table-column>
      <el-table-column label="授权结果" align="center" prop="authResult">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.auth_result" :value="scope.row.authResult"/>
        </template>
      </el-table-column>
      <el-table-column label="错误信息" align="center" prop="errorMsg" width="200" />
      <el-table-column label="IP地址" align="center" prop="ipAddress" width="120" />
      <el-table-column label="用户代理" align="center" prop="userAgent" width="200" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row)"
            v-hasPermi="['wx:authlog:query']"
          >查看</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['wx:authlog:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['wx:authlog:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改微信授权日志对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="OpenID" prop="openid">
          <el-input v-model="form.openid" placeholder="请输入微信OpenID" />
        </el-form-item>
        <el-form-item label="授权类型" prop="authType">
          <el-select v-model="form.authType" placeholder="请选择授权类型">
            <el-option
              v-for="dict in dict.type.auth_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="授权结果" prop="authResult">
          <el-radio-group v-model="form.authResult">
            <el-radio
              v-for="dict in dict.type.auth_result"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="错误信息" prop="errorMsg">
          <el-input v-model="form.errorMsg" type="textarea" placeholder="请输入错误信息" />
        </el-form-item>
        <el-form-item label="IP地址" prop="ipAddress">
          <el-input v-model="form.ipAddress" placeholder="请输入IP地址" />
        </el-form-item>
        <el-form-item label="用户代理" prop="userAgent">
          <el-input v-model="form.userAgent" type="textarea" placeholder="请输入用户代理" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 日志详情对话框 -->
    <el-dialog title="授权日志详情" :visible.sync="viewOpen" width="800px" append-to-body>
      <el-descriptions :column="2" border>
        <el-descriptions-item label="日志ID">{{ viewForm.id }}</el-descriptions-item>
        <el-descriptions-item label="OpenID">{{ viewForm.openid }}</el-descriptions-item>
        <el-descriptions-item label="授权类型">
          <dict-tag :options="dict.type.auth_type" :value="viewForm.authType"/>
        </el-descriptions-item>
        <el-descriptions-item label="授权结果">
          <dict-tag :options="dict.type.auth_result" :value="viewForm.authResult"/>
        </el-descriptions-item>
        <el-descriptions-item label="错误信息" :span="2">{{ viewForm.errorMsg }}</el-descriptions-item>
        <el-descriptions-item label="IP地址">{{ viewForm.ipAddress }}</el-descriptions-item>
        <el-descriptions-item label="用户代理" :span="2">{{ viewForm.userAgent }}</el-descriptions-item>
        <el-descriptions-item label="创建时间">{{ parseTime(viewForm.createTime) }}</el-descriptions-item>
        <el-descriptions-item label="备注" :span="2">{{ viewForm.remark }}</el-descriptions-item>
      </el-descriptions>
    </el-dialog>

    <!-- 清理日志对话框 -->
    <el-dialog title="清理过期日志" :visible.sync="cleanOpen" width="400px" append-to-body>
      <el-form ref="cleanForm" :model="cleanForm" label-width="100px">
        <el-form-item label="保留天数" prop="days">
          <el-input-number v-model="cleanForm.days" :min="1" :max="365" placeholder="请输入保留天数" />
          <span style="margin-left: 10px; color: #999;">天前的日志将被删除</span>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitClean">确 定</el-button>
        <el-button @click="cancelClean">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listWxAuthLog, getWxAuthLog, delWxAuthLog, addWxAuthLog, updateWxAuthLog, cleanExpiredLogs } from "@/api/wx/authlog";

export default {
  name: "WxAuthLog",
  dicts: ['auth_type', 'auth_result'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 微信授权日志表格数据
      wxAuthLogList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否显示查看弹出层
      viewOpen: false,
      // 是否显示清理弹出层
      cleanOpen: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        openid: null,
        authType: null,
        authResult: null
      },
      // 表单参数
      form: {},
      // 查看表单参数
      viewForm: {},
      // 清理表单参数
      cleanForm: {
        days: 30
      },
      // 表单校验
      rules: {
        openid: [
          { required: true, message: "OpenID不能为空", trigger: "blur" }
        ],
        authType: [
          { required: true, message: "授权类型不能为空", trigger: "change" }
        ],
        authResult: [
          { required: true, message: "授权结果不能为空", trigger: "change" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询微信授权日志列表 */
    getList() {
      this.loading = true;
      listWxAuthLog(this.queryParams).then(response => {
        this.wxAuthLogList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 取消清理
    cancelClean() {
      this.cleanOpen = false;
      this.cleanForm.days = 30;
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        openid: null,
        authType: null,
        authResult: null,
        errorMsg: null,
        ipAddress: null,
        userAgent: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加微信授权日志";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getWxAuthLog(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改微信授权日志";
      });
    },
    /** 查看按钮操作 */
    handleView(row) {
      getWxAuthLog(row.id).then(response => {
        this.viewForm = response.data;
        this.viewOpen = true;
      });
    },
    /** 清理日志按钮操作 */
    handleClean() {
      this.cleanOpen = true;
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateWxAuthLog(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addWxAuthLog(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 提交清理 */
    submitClean() {
      cleanExpiredLogs(this.cleanForm.days).then(response => {
        this.$modal.msgSuccess("清理成功");
        this.cleanOpen = false;
        this.getList();
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除微信授权日志编号为"' + ids + '"的数据项？').then(function() {
        return delWxAuthLog(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('wx/authlog/export', {
        ...this.queryParams
      }, `wx_auth_log_${new Date().getTime()}.xlsx`)
    }
  }
};
</script> 